package com.tf.integrated.config;

import com.ccssoft.m8.common.ccssoft.modules.util.EntityWrapperHandle;
import com.tf.integrated.entity.*;
import com.tf.integrated.service.*;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.List;

public class ResultWebSocketClient extends WebSocketClient
{
    private static final Logger LOGGER = LoggerFactory.getLogger(ResultWebSocketClient.class);
    private String mDevic = null;
    private EventresultvehicleinfoService<EventresultvehicleinfoEntity> vehicleService;
    private EventresultpersonService<EventresultpersonEntity> personService;
    private EventresulttricyclesinfoService<EventresulttricyclesinfoEntity> tricycleService;
    private EventresultpedestriansinfoService<EventresultpedestriansinfoEntity> pedestrianService;
    private EventresultbikesinfoService<EventresultbikesinfoEntity> bikeService;
    private VehicleStyleService<VehicleStyleEntity> vehicleStyleService;

    public ResultWebSocketClient(URI serverUri) {
        super(serverUri);
    }

    public void setPersonService(EventresultpersonService<EventresultpersonEntity> personService)
    {
        this.personService = personService;
    }

    public void setVehicleService(EventresultvehicleinfoService<EventresultvehicleinfoEntity> vehicleService)
    {
        this.vehicleService = vehicleService;
    }

    public void setTricycleService(EventresulttricyclesinfoService<EventresulttricyclesinfoEntity> tricycleService)
    {
        this.tricycleService = tricycleService;
    }

    public void setPedestrianService(EventresultpedestriansinfoService<EventresultpedestriansinfoEntity> pedestrianService)
    {
        this.pedestrianService = pedestrianService;
    }

    public void setBikeService(EventresultbikesinfoService<EventresultbikesinfoEntity> bikeService)
    {
        this.bikeService = bikeService;
    }

    public void setVehicleStyleService(VehicleStyleService<VehicleStyleEntity> vehicleStyleService) {
        this.vehicleStyleService = vehicleStyleService;
    }

    public void setmDevic(String mDevic)
    {
        this.mDevic = mDevic;
    }

    @Override
    public void onOpen(ServerHandshake arg0)
    {
        // TODO Auto-generated method stub
        LOGGER.info("------ ResultWebSocketClient onOpen ------");
    }

    @Override
    public void onClose(int arg0, String arg1, boolean arg2)
    {
        // TODO Auto-generated method stub
        LOGGER.info("------ ResultWebSocketClient onClose ------{}",arg1);
    }

    @Override
    public void onError(Exception arg0)
    {
        // TODO Auto-generated method stub
        LOGGER.info("------ ResultWebSocketClient onError ------{}",arg0);
    }
    @Override
    public void onMessage(String arg0) {
        // TODO Auto-generated method stub
        //LOGGER.info("--------ResultWebSocketClient 接收到服务端数据String： " + arg0 + "--------");
    }

    @Override
    public void onMessage(ByteBuffer arg0)
    {
        byte[] arr = new byte[arg0.remaining()];
        arg0.get(arr);
        // TODO Auto-generated method stub
        //LOGGER.info("-------- ResultWebSocketClient 接收到服务端数据ByteBuffer： " + arr + "--------");
        int headlen = Config.readInt32BE(arr,0);
        String headval = Config.readString(arr,4,headlen);
        LOGGER.info("-------- ResultWebSocketClient headval： " + headval + "--------");
        try
        {
            JSONObject jsonhead = new JSONObject(headval);
            int Channel = jsonhead.optInt("Channel");
            int ResultLen = jsonhead.optInt("ResultLen");
            int ObjPictureLen = jsonhead.optInt("ObjPictureLen");
            int FullPictureLen = jsonhead.optInt("FullPictureLen");
            int ResultType = jsonhead.optInt("ResultType");
            String ObjPicturePath = null;
            String FullPicturePath = null;
            String Resultval = Config.readString(arr,headlen+4,ResultLen);
            if (ObjPictureLen>0)
            {
                byte[] buff = new byte[ObjPictureLen];
                for (int i = 0; i < buff.length; i++)
                {
                    buff[i] = arr[headlen+4+ResultLen+i];
                }
                String  tmpfilename = "OBJP"+System.currentTimeMillis()+".jpg";
                String  tmppath = Config.getMimgpath() + tmpfilename;
                Config.saveFile(tmppath,buff);
                ObjPicturePath = Config.getMurlpath() + tmpfilename;
            }
            if (FullPictureLen>0)
            {
                byte[] buff = new byte[FullPictureLen];
                for (int i = 0; i < buff.length; i++)
                {
                    buff[i] = arr[headlen+4+ResultLen+ObjPictureLen+i];
                }
                String  tmpfilename = "FULLP"+System.currentTimeMillis()+".jpg";
                String  tmppath = Config.getMimgpath() + tmpfilename;
                Config.saveFile(tmppath,buff);
                FullPicturePath = Config.getMurlpath() + tmpfilename;
            }
            //LOGGER.info("-------- ResultWebSocketClient ResultType： " + ResultType + "--------");
            if(ResultType==1)
            {
                parseMessgedatavehicleinfo(Resultval,ObjPicturePath,FullPicturePath);
            }
            else if(ResultType==2)
            {
                parseMessgedatatricyclesinfo(Resultval,ObjPicturePath,FullPicturePath);
            }
            else if(ResultType==3)
            {
                parseMessgedatapedestriansinfo(Resultval,ObjPicturePath,FullPicturePath);
            }
            else if(ResultType==4)
            {
                parseMessgedatabikesinfo(Resultval,ObjPicturePath,FullPicturePath);
            }
            else if(ResultType==5)
            {
                parseMessgedataperson(Resultval,ObjPicturePath,FullPicturePath);
            }
        }
        catch (Exception e)
        {

        }
    }
    //解析订阅结构化数据(非机动车)
    private void parseMessgedatabikesinfo(String Resultval,String objpath,String fullpath)
    {
        if (Resultval != null && Resultval.equals("")==false)
        {
            try
            {
                EventresultbikesinfoEntity evvehety = new EventresultbikesinfoEntity();
                JSONObject Resultvaljson = new JSONObject(Resultval);
                String Time = Resultvaljson.optString("Time");
                evvehety.setTime(Time);
                String Rect = Resultvaljson.optJSONArray("Rect").toString();
                evvehety.setRect(Rect);
                String Chn = Resultvaljson.optString("Chn");
                evvehety.setChn(Chn);
                String Bag = Resultvaljson.optJSONObject("Bag").toString();
                evvehety.setBag(Bag);
                String BottomColor = Resultvaljson.optJSONObject("BottomColor").toString();
                evvehety.setBottomcolor(BottomColor);
                String BottomType = Resultvaljson.optJSONObject("BottomType").toString();
                evvehety.setBottomtype(BottomType);
                String DriveWay = Resultvaljson.optJSONObject("DriveWay").toString();
                evvehety.setDriveway(DriveWay);
                String Glasses = Resultvaljson.optJSONObject("Glasses").toString();
                evvehety.setGlasses(Glasses);
                String Hair = Resultvaljson.optJSONObject("Hair").toString();
                evvehety.setHair(Hair);
                String Hat = Resultvaljson.optJSONObject("Hat").toString();
                evvehety.setHat(Hat);
                String Age = Resultvaljson.optJSONObject("Age").toString();
                evvehety.setAge(Age);
                String Baby = Resultvaljson.optJSONObject("Baby").toString();
                evvehety.setBaby(Baby);
                String Knapsack = Resultvaljson.optJSONObject("Knapsack").toString();
                evvehety.setKnapsack(Knapsack);
                String Mask = Resultvaljson.optJSONObject("Mask").toString();
                evvehety.setMask(Mask);
                String MessengerBag = Resultvaljson.optJSONObject("MessengerBag").toString();
                evvehety.setMessengerbag(MessengerBag);
                String Orientation = Resultvaljson.optJSONObject("Orientation").toString();
                evvehety.setOrientation(Orientation);
                String Sex = Resultvaljson.optJSONObject("Sex").toString();
                evvehety.setSex(Sex);
                String ShoulderBag = Resultvaljson.optJSONObject("ShoulderBag").toString();
                evvehety.setShoulderaag(ShoulderBag);
                String Type = Resultvaljson.optJSONObject("Type").toString();
                evvehety.setType(Type);
                String Umbrella = Resultvaljson.optJSONObject("Umbrella").toString();
                evvehety.setUmbrella(Umbrella);
                String UpperColor = Resultvaljson.optJSONObject("UpperColor").toString();
                evvehety.setUppercolor(UpperColor);
                String UpperTexture = Resultvaljson.optJSONObject("UpperTexture").toString();
                evvehety.setUppertexture(UpperTexture);
                String UpperType = Resultvaljson.optJSONObject("UpperType").toString();
                evvehety.setUppertype(UpperType);
                evvehety.setFullpicpath(fullpath);
                evvehety.setObjpicpath(objpath);
                evvehety.setDeviceid(mDevic);
                bikeService.insert(evvehety);
                LOGGER.info("-------- parseMessgedatabikesinfo mDevic： " + mDevic + "--------");
            }
            catch (Exception e)
            {

            }
        }
    }
    //解析订阅结构化数据(行人)
    private void parseMessgedatapedestriansinfo(String Resultval,String objpath,String fullpath)
    {
        if (Resultval != null && Resultval.equals("")==false)
        {
            try
            {
                EventresultpedestriansinfoEntity evvehety = new EventresultpedestriansinfoEntity();
                JSONObject Resultvaljson = new JSONObject(Resultval);
                String Time = Resultvaljson.optString("Time");
                evvehety.setTime(Time);
                String Rect = Resultvaljson.optJSONArray("Rect").toString();
                evvehety.setRect(Rect);
                String Chn = Resultvaljson.optString("Chn");
                evvehety.setChn(Chn);
                String Age = Resultvaljson.optJSONObject("Age").toString();
                evvehety.setAge(Age);
                String Baby = Resultvaljson.optJSONObject("Baby").toString();
                evvehety.setBaby(Baby);
                String Bag = Resultvaljson.optJSONObject("Bag").toString();
                evvehety.setBag(Bag);
                String BottomColor = Resultvaljson.optJSONObject("BottomColor").toString();
                evvehety.setBottomcolor(BottomColor);
                String BottomType = Resultvaljson.optJSONObject("BottomType").toString();
                evvehety.setBottomtype(BottomType);
                String DriveWay = Resultvaljson.optJSONObject("DriveWay").toString();
                evvehety.setDriveway(DriveWay);
                String Glasses = Resultvaljson.optJSONObject("Glasses").toString();
                evvehety.setGlasses(Glasses);
                String Hair = Resultvaljson.optJSONObject("Hair").toString();
                evvehety.setHair(Hair);
                String Hat = Resultvaljson.optJSONObject("Hat").toString();
                evvehety.setHat(Hat);
                String Knapsack = Resultvaljson.optJSONObject("Knapsack").toString();
                evvehety.setKnapsack(Knapsack);
                String Mask = Resultvaljson.optJSONObject("Mask").toString();
                evvehety.setMask(Mask);
                String MessengerBag = Resultvaljson.optJSONObject("MessengerBag").toString();
                evvehety.setMessengerbag(MessengerBag);
                String Orientation = Resultvaljson.optJSONObject("Orientation").toString();
                evvehety.setOrientation(Orientation);
                String Sex = Resultvaljson.optJSONObject("Sex").toString();
                evvehety.setSex(Sex);
                String ShoulderBag = Resultvaljson.optJSONObject("ShoulderBag").toString();
                evvehety.setShoulderaag(ShoulderBag);
                String Type = Resultvaljson.optJSONObject("Type").toString();
                evvehety.setType(Type);
                String Umbrella = Resultvaljson.optJSONObject("Umbrella").toString();
                evvehety.setUmbrella(Umbrella);
                String UpperColor = Resultvaljson.optJSONObject("UpperColor").toString();
                evvehety.setUppercolor(UpperColor);
                String UpperTexture = Resultvaljson.optJSONObject("UpperTexture").toString();
                evvehety.setUppertexture(UpperTexture);
                String UpperType = Resultvaljson.optJSONObject("UpperType").toString();
                evvehety.setUppertype(UpperType);
                evvehety.setFullpicpath(fullpath);
                evvehety.setObjpicpath(objpath);
                evvehety.setDeviceid(mDevic);
                pedestrianService.insert(evvehety);
                LOGGER.info("-------- parseMessgedatapedestriansinfo mDevic： " + mDevic + "--------");
            }
            catch (Exception e)
            {

            }
        }
    }
    //解析订阅结构化数据(三轮车)
    private void parseMessgedatatricyclesinfo(String Resultval,String objpath,String fullpath)
    {
        if (Resultval != null && Resultval.equals("")==false)
        {
            try
            {
                EventresulttricyclesinfoEntity evvehety = new EventresulttricyclesinfoEntity();
                JSONObject Resultvaljson = new JSONObject(Resultval);
                String Convertible = Resultvaljson.optJSONObject("Convertible").toString();
                evvehety.setConvertible(Convertible);
                String Chn = Resultvaljson.optString("Chn");
                evvehety.setChn(Chn);
                String RoofColor = Resultvaljson.optJSONObject("RoofColor").toString();
                evvehety.setRoofcolor(RoofColor);
                String Type = Resultvaljson.optJSONObject("Type").toString();
                evvehety.setType(Type);
                String DriveWay = Resultvaljson.optJSONObject("DriveWay").toString();
                evvehety.setDriveway(DriveWay);
                String Manned = Resultvaljson.optJSONObject("Manned").toString();
                evvehety.setManned(Manned);
                String Time = Resultvaljson.optString("Time");
                evvehety.setTime(Time);
                String Rect = Resultvaljson.optJSONArray("Rect").toString();
                evvehety.setRect(Rect);
                evvehety.setFullpicpath(fullpath);
                evvehety.setObjpicpath(objpath);
                evvehety.setDeviceid(mDevic);
                tricycleService.insert(evvehety);
                LOGGER.info("-------- parseMessgedatatricyclesinfo mDevic： " + mDevic + "--------");
            }
            catch (Exception e)
            {

            }
        }
    }
    //解析订阅结构化数据(人脸)
    private void parseMessgedataperson(String Resultval,String objpath,String fullpath)
    {
        if (Resultval != null && Resultval.equals("")==false)
        {
            try
            {
                EventresultpersonEntity evvehety = new EventresultpersonEntity();
                JSONObject Resultvaljson = new JSONObject(Resultval);
                String Age = Resultvaljson.optJSONObject("Age").toString();
                evvehety.setAge(Age);
                String Chn = Resultvaljson.optString("Chn");
                evvehety.setChn(Chn);
                evvehety.setFullpicpath(fullpath);
                evvehety.setObjpicpath(objpath);
                String Time = Resultvaljson.optString("Time");
                evvehety.setTime(Time);
                String Rect = Resultvaljson.optJSONArray("Rect").toString();
                evvehety.setRect(Rect);
                String Glasses = Resultvaljson.optJSONObject("Glasses").toString();
                evvehety.setGlasses(Glasses);
                String Hair = Resultvaljson.optJSONObject("Hair").toString();
                evvehety.setHair(Hair);
                String Hat = Resultvaljson.optJSONObject("Hat").toString();
                evvehety.setHat(Hat);
                String Mask = Resultvaljson.optJSONObject("Mask").toString();
                evvehety.setMask(Mask);
                String RegionName = Resultvaljson.optJSONObject("RegionName").toString();
                evvehety.setRegionname(RegionName);
                String Sex = Resultvaljson.optJSONObject("Sex").toString();
                evvehety.setSex(Sex);
                String Type = Resultvaljson.optJSONObject("Type").toString();
                evvehety.setType(Type);
                evvehety.setDeviceid(mDevic);
                personService.insert(evvehety);
                LOGGER.info("-------- parseMessgedataperson mDevic： " + mDevic + "--------");
            }
            catch (Exception e)
            {

            }
        }
    }
    //解析订阅结构化数据(机动车)
    private void parseMessgedatavehicleinfo(String Resultval,String objpath,String fullpath)
    {
        if (Resultval != null && Resultval.equals("")==false)
        {
            try
            {
                EventresultvehicleinfoEntity evvehety = new EventresultvehicleinfoEntity();
                JSONObject Resultvaljson = new JSONObject(Resultval);
                String CarType = Resultvaljson.optJSONObject("CarType").toString();
                evvehety.setCartype(CarType);
                String Chn = Resultvaljson.optString("Chn");
                evvehety.setChn(Chn);
                String CoDriver = Resultvaljson.optJSONObject("CoDriver").toString();
                evvehety.setCodriver(CoDriver);
                String CoNoBelt = Resultvaljson.optJSONObject("CoNoBelt").toString();
                evvehety.setConobelt(CoNoBelt);
                String Color = Resultvaljson.optJSONObject("Color").toString();
                evvehety.setColor(Color);
                String DriveWay = Resultvaljson.optJSONObject("DriveWay").toString();
                evvehety.setDriveway(DriveWay);
                String Drop = Resultvaljson.optJSONObject("Drop").toString();
                evvehety.setDrop(Drop);
                String HasCall = Resultvaljson.optJSONObject("HasCall").toString();
                evvehety.setHascall(HasCall);
                String HasCrash = Resultvaljson.optJSONObject("HasCrash").toString();
                evvehety.setHascrash(HasCrash);
                String HasRack = Resultvaljson.optJSONObject("HasRack").toString();
                evvehety.setHasrack(HasRack);
                String HasSpareTire = Resultvaljson.optJSONObject("HasSpareTire").toString();
                evvehety.setHassparetire(HasSpareTire);
                String HasSunroof = Resultvaljson.optJSONObject("HasSunroof").toString();
                evvehety.setHassunroof(HasSunroof);
                String IsDanger = Resultvaljson.optJSONObject("IsDanger").toString();
                evvehety.setIsdanger(IsDanger);
                String IsSlag = Resultvaljson.optJSONObject("IsSlag").toString();
                evvehety.setIsslag(IsSlag);
                String MainNoBelt = Resultvaljson.optJSONObject("MainNoBelt").toString();
                evvehety.setMainnobelt(MainNoBelt);
                String MarkerNum = Resultvaljson.optJSONObject("MarkerNum").toString();
                evvehety.setMarkernum(MarkerNum);
                String Orientation = Resultvaljson.optJSONObject("Orientation").toString();
                evvehety.setOrientation(Orientation);
                String Paper = Resultvaljson.optJSONObject("Paper").toString();
                evvehety.setPaper(Paper);
                String PlateInfo = Resultvaljson.optJSONObject("PlateInfo").toString();
                evvehety.setPlateinfo(PlateInfo);
                String Rect = Resultvaljson.optJSONArray("Rect").toString();
                evvehety.setRect(Rect);
                String StyleName = Resultvaljson.optString("StyleName");
                evvehety.setStylename(StyleName);
                evvehety.setFullpicpath(fullpath);
                evvehety.setObjpicpath(objpath);
                String Time = Resultvaljson.optString("Time");
                evvehety.setTime(Time);
                String Type = Resultvaljson.optJSONObject("Type").toString();
                evvehety.setType(Type);
                evvehety.setDeviceid(mDevic);
                EntityWrapperHandle<VehicleStyleEntity> ew =new EntityWrapperHandle<VehicleStyleEntity>();
                ew.eq("name",StyleName);
                List<VehicleStyleEntity> list = vehicleStyleService.selectList(ew);
                if(list!=null&&list.size()>0){
                    evvehety.setFamily(list.get(0).getFamily());
                    evvehety.setBrand(list.get(0).getBrand());
                    evvehety.setCode(list.get(0).getCode());
                }
                vehicleService.insert(evvehety);
                LOGGER.info("-------- parseMessgedatavehicleinfo mDevic： " + mDevic + "--------");
            }
             catch (Exception e)
            {

            }
        }
    }
}
